package 前端数据访问模拟;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.ToolUtils;
import com.bbcare.exportExcel.plat.utils.Json2Map;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import patientimport.Shujuyuan;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class 模拟数据 {
    private static Logger logger = Logger.getLogger(模拟数据.class);
    static String log4jPath = "E:\\测试项目\\1.0\\src\\test\\java\\前端数据访问模拟\\log4j.properties";

    static String sessionId = "?sessionId=e8bae65b-53e5-4f36-832a-7b9de4261b67";
    static String queueId =  "12fzszfzzdqueue001";//"12fzszckzdqueue001";
    static String taskName= "第6月随访问卷";
    static String followupAge = "6月龄";






    public static void main(String[] args)throws Exception {

        PropertyConfigurator.configureAndWatch(log4jPath, 60000);

        //第一步 读取excel数据
        Excel_reader test = new Excel_reader();
         List<String> arr = test.getRowData("F:\\2019-11-7\\2019-11-7\\辅助-6个月问卷汇总+过敏.xlsx", 6);  //后面的参数代表需要输出哪些列，参数个数可以任意
        System.out.println(arr.get(0));
        arr.remove(0);
        List<String> arr2  = new ArrayList<>();
        arr2.add("SA0380");
         List<String> err = new ArrayList<>();
         List<String> miss = new ArrayList<>();
        List<String> success = new ArrayList<>();
        //第二步 模拟用户消息
        //1.获取用户基本信息
        for (String code :arr) {
            try {


            System.out.println("开始处理"+code);
            logger.info("开始处理"+code);
            String url1 = "http://doctor.bb-care.com/followup/service/patient/patientQueueListQuery"+sessionId;
            String data1 = "{\"data\":{\"page\":1,\"rows\":20,\"flag\":\"2\"," +
                    "\"systemType\":\"00\",\"queueId\":\""+queueId+"\",\"fuzzyVar\":\""+code+"\"}}";
            String patientSchemeId = null;
            String patientId = null;
            String followQueueId = null;
            String patientName = null;
            //System.out.printf(Shujuyuan.httpPostCall(data1,url1).toJSONString());
            JSONObject jsonData =  Shujuyuan.httpPostCall(data1,url1);
            JSONArray jsonArray = jsonData.getJSONObject("data").getJSONArray("rows");
            for (Object x : jsonArray) {
                JSONObject jsonObject  =  JSON.parseObject(x.toString());
                patientSchemeId =  jsonObject.getString("patientSchemeId");
                patientId =  jsonObject.getString("patientId");
                followQueueId =  jsonObject.getString("followQueueId");
                patientName =  jsonObject.getString("patientName");
                patientSchemeId =  jsonObject.getString("patientSchemeId");

            }
            if(patientSchemeId == null ||patientId == null || followQueueId==null ){
                logger.error( "数据错误：code : "+code+" patientSchemeId :"+ patientSchemeId + "patientId :"+ patientId + "followQueueId:" +followQueueId);
                miss.add(code);
                continue;
            }

            //2.获取任务列表
            String url2 = "http://doctor.bb-care.com/followup/service/patient/patientTaskListQuery"+sessionId;
            String data2 ="{\"data\":{\"followQueueId\":\""+followQueueId+"\",\"patientSchemeId\":\""+patientSchemeId+"\"}}";
            JSONObject jsonData2 =  Shujuyuan.httpPostCall(data2,url2);
            // System.out.println(jsonData2.toJSONString());

            //3.获得任务详情
            JSONArray jsonArray2 =  jsonData2.getJSONObject("data").getJSONArray("rows");
            JSONObject jsonObject3 = null;
            for (Object x:jsonArray2 ) {
                if(taskName.equals(JSON.parseObject(x.toString()).getString("taskName"))){
                    jsonObject3 = JSON.parseObject(x.toString());
                }
            }
            String taskDate = jsonObject3.getString("taskDate");
            String ptSchemeId = jsonObject3.getString("ptSchemeId");
            String seqNo = jsonObject3.getString("seqNo");
            String ptTaskId = jsonObject3.getString("taskId");
            //System.out.println(jsonObject3.toString());



            String url3= "http://doctor.bb-care.com/followup/service/patient/patientTaskContentDataQuery"+sessionId;
            String data3 = "{\"data\":{\"followQueueId\":\""+followQueueId+"\",\"ptTaskId\":\""+ptTaskId+"\"}}";

            JSONObject jsonData3 =  Shujuyuan.httpPostCall(data3,url3);


            //System.out.println(jsonData3.toJSONString());
            JSONArray jsonArray3 = jsonData3.getJSONObject("data").getJSONObject("task").getJSONArray("ptContentIds");
            JSONArray jsonPtContentIds = new JSONArray();
            for (Object x :jsonArray3){
                JSONObject jsonObject = new JSONObject(new LinkedHashMap());
                jsonObject.put("templType",JSON.parseObject(x.toString()).getString("templType"));
                jsonObject.put("contentId", JSON.parseObject(x.toString()).getString("contentId"));
                jsonPtContentIds.add(jsonObject);
            }
            //System.out.println(jsonPtContentIds.toJSONString());

            //4.修改数据
            String url4 = "http://doctor.bb-care.com/followup/service/patient/patientTaskContentDataModify"+sessionId;
            String data4 = "{\"data\":{\"oprInfo\":{\"followQueueId\":\""+followQueueId+"\",\"opCode\":\"1001\",\"opNote\":\"【演示医生】医生为【"+patientName+"】患者进行"+taskName+"修改\",\"" +
                    "opType\":\"2\",\"sourceType\":\"2\",\"patientId\":\""+patientId+"\",\"ptSchemeId\":\""+ptSchemeId+"\"},\"questiones\":{\"fzszfzzd_sf_fmfs\":\"ipad\",\"fzszfzzd_sf_zyqk\":\"\"}" +
                    ",\"task\":{\"ptContentIds\":"+jsonPtContentIds.toJSONString()+",\"ptTaskId\":\""+ptTaskId+"\"" +
                    ",\"ptTaskName\":\""+taskName+"\",\"seqNo\":"+seqNo+",\"finishDate\":\""+taskDate+"\",\"nextDate\":\"\",\"nextAddress\":\"上海市华山路1961号\",\"updateAuname\":\"演示医生\",\"followupAge\":\""+followupAge+"\",\"taskNote\":\"\"},\"paramsRecord\":{}}}";
           // System.out.println(data4);
            JSONObject jsonData4 =  Shujuyuan.httpPostCall(data4,url4);
            logger.info("处理完成"+jsonData4.toString());
            if("true".equals(jsonData4.getString("success"))){
                success.add(code);
            }else{
                err.add(code);
            }

            //System.out.println(jsonData4.toString());

            }catch (Exception ex){
                err.add(code);
            }
        }

        logger.info("成功数量：" +success.size());
        logger.info("数据:"+ ToolUtils.listToString(success));
        logger.info("失败数量：" +err.size());
        logger.info("数据:"+ ToolUtils.listToString(err));
        logger.info("缺失数量：" +miss.size());
        logger.info("数据:"+ ToolUtils.listToString(miss));
    }







}
